package config

import (
	"time"

	"github.com/BurntSushi/toml"
)

// LoadGlobalConfig
func LoadGlobalConfig(path string) (c *Config, err error) {
	c, err = ParseConfig(path)
	if err != nil {
		return
	}
	return
}

// ParseConfig
func ParseConfig(path string) (*Config, error) {
	var c Config
	_, err := toml.DecodeFile(path, &c)
	if err != nil {
		return nil, err
	}
	return &c, nil
}

// Config
type Config struct {
	Project   Project   `toml:"project"`
	Redis     Redis     `toml:"redis"`
	RunParams RunParams `toml:"runparams"`
	Log       Log       `toml:"log"`
}

// RunParam
type RunParams struct {
	ServiceType        string        `toml:"ServiceType"`
	TaskApi            string        `toml:"TaskApi"`
	RobotAccountsCount int           `toml:"RobotAccountsCount"`
	GetOrderInterval   time.Duration `toml:"GetOrderInterval"`
	MnemonicPath       string        `toml:"MnemonicPath"`
	ExportAccountPath  string        `toml:"ExportAccountPath"`
	NetWorkInfoPath    string        `toml:"NetWorkInfoPath"`
	TokenInfoPath      string        `toml:"TokenInfoPath"`
	SwapTokenPath      string        `toml:"SwapTokenPath"`
	ABIInfoPath        string        `toml:"ABIInfoPath"`
	CodeHelper         string        `toml:"CodeHelper"`
	EmailNotifyAccount string        `toml:"EmailNotifyAccount"`
}

// Project
type Project struct {
	Name string `toml:"name"`
}

// Redis redis config
type Redis struct {
	Addr         string `toml:"addr"`
	Password     string `toml:"password"`
	Database     int    `toml:"database"`
	MaxOpenConns int    `toml:"maxOpenConns"`
	MaxIdleConns int    `toml:"maxIdleConns"`
}

// Log
type Log struct {
	Level    int    `toml:"level"`
	Path     string `toml:"path"`
	MaxDays  int    `toml:"maxdays"`
	Separate string `toml:"separate"`
}

type TokenInfo struct {
	ChainID  int    `json:"chainId"`
	Address  string `json:"address"`
	Name     string `json:"name"`
	Symbol   string `json:"symbol"`
	Decimals int    `json:"decimals"`
	LogoURI  string `json:"logoURI"`
	SwapPath string `json:"swappath"`
}

type NetWorkInfo struct {
	Name              string
	ChainID           int
	MainCoinName      string
	RpcUrls           []string
	RestEndpoint      string
	WsNodeUrl         string
	DefaultNode       string
	DefaultBalancer   string
	StableCoin        string
	StableCoinDecimal int
	PayDB             string
	ExecBots          []string
	MinServiceCharge  float64
	MinNodeBalance    float64
	TrComfirmTime     time.Duration
}
